package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.db.Freeze;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-31 04:18
 **/
@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private videoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private FreezeApi freezeApi;

    ///分页查询mysql中的userInfo所有
    public PageResult users(Integer page, Integer pagesize) {
        //1.调用API查询用户数据
        IPage iPage = userInfoApi.findAll(page,pagesize);
        //2.构造返回
        return new PageResult(page,pagesize,iPage.getTotal(),iPage.getRecords());
    }


//    查询用户基本资料
    public UserInfo findUserById(Long userId) {
        UserInfo user = userInfoApi.findUser(userId);
        return user;
    }

    //根据用户id分页查询用户发布的视频列表
    public PageResult findVideoByUserId(Long uid, Integer page, Integer pagesize) {
        //1.调用api分页查询视频列表  --items存放的是List<Video>
        PageResult result = videoApi.findByUserId(uid,page,pagesize);
        //获取视频列表
        List<Video> items = (List<Video>) result.getItems();
        //视频列表是否存在
        if(CollUtil.isEmpty(items)){
            return result;
        }
        UserInfo userInfo = userInfoApi.findUser(uid);
        List<VideoVo> list = new ArrayList<>();
        //一个video转化一个vo
        for (Video video : items) {
            VideoVo vo = VideoVo.init(userInfo, video);//动态发布人的userInfo,video对象
            list.add(vo);
        }
        //构造返回
        result.setItems(list);
        return result;
    }

    //查询动态列表
    public PageResult findMovement(Long uid, Integer status, Integer page, Integer pagesize) {

        //1.调用API查询动态列表  PageResult -- items --List<Movement>
         PageResult result  = movementApi.findMovements(uid,status,page,pagesize);

        //2.获取动态列表
        List<Movement> items = (List<Movement>) result.getItems();
        //3.判断列表是否存在
        if(CollUtil.isEmpty(items)){
            return result;
        }
        List<MovementsVo> list = new ArrayList<>();
         //4.一个Movement构造一个MovementVo
        for (Movement item : items) {
            Long userId = item.getUserId();
            UserInfo user = userInfoApi.findUser(userId);
            MovementsVo vo = MovementsVo.init(user, item);//动态发布人详情，动态对象
            list.add(vo);
        }
        //5.构造返回
        result.setItems(list);
        return result;
    }

    //查看动态详情
    public MovementsVo findMonements(String id) {
        Movement vo =  movementApi.findDetails(id);
        UserInfo user = userInfoApi.findUser(vo.getUserId());
        //构建返回
        MovementsVo init = MovementsVo.init(user, vo);
        return init;
    }

    // 查看动态评论列表 翻页
    public PageResult findByComments(Integer page, Integer pagesize, String id) {
       PageResult pageResult  =  commentApi.findByComment(id,page,pagesize);
       if(pageResult.getCounts() == 0){
           return  new PageResult();
       }
        List<Comment> commentList = (List<Comment>) pageResult.getItems();
        List<Long> userId = CollUtil.getFieldValues(commentList, "userId", Long.class);
        //查询对象
        Map<Long, UserInfo> map = userInfoApi.findByIds(userId);

        //构建vo
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = map.get(comment.getUserId());
            CommentVo init = CommentVo.init(userInfo, comment);
            list.add(init);
        }
        pageResult.setItems(list);
        return pageResult;


    }
    // 动态通过 或 拒绝
    public Map passAndReject(String[] ids,Integer state) {
        movementApi.updateStates(ids,state);
        Map map = new HashMap();
        map.put("message", "操作成功");
        return map;
    }


    //用户冻结
    public void userFreeze(Freeze freeze) {
        UserInfo user = userInfoApi.findUser(freeze.getUserId());
        user.setUserStatus("2");
        userInfoApi.update(user);
        freezeApi.freezeUser(freeze);


    }

    public void unfreeze(String userId) {
        Long id=Long.parseLong(userId);
        UserInfo user = userInfoApi.findUser(id);
        user.setUserStatus("1");
        userInfoApi.update(user);
        freezeApi.deleteUser(id);
    }
}
